/* Copyright 2012 Oliver Katz

    This file is part of Theta.

    Theta is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef __THETA_VM_VM_H
#define __THETA_VM_VM_H

#include "bytecode.h"
#include "report.h"
#include "../api/core.h"
#include "../data/data.h"
#include "../parsing/frame.h"
#include "../parsing/parsing.h"
#include "../compiler/dyndir.h"

namespace vm
{
	using namespace api;
	using namespace parsing;
	using namespace compiler;
	using namespace compiler::dyndir;
	
	class VM
	{
	private:
		string name;
		t_data_meta_page data;
		map<string, Clause> clauses;
		map<string, map<string, vector<string> > > cfg;
		DynamicDirectory midcomp;
		map<string, BytecodeClause> bc;
		Parser parser;
		map<string, int> vartable;
		map<int, bool> regtable;

		bool showProgress;
		bool debugMidComp;

		void generateMidComp();
		void reloadMidComp();

		int storeAddress(string n, int a);
		int getAddress(string n);
		int allocRegister();
		void freeRegister(int r);

		BytecodeClause generateFrame(Frame f);

	public:
		VM();
		void enableShowProgress() {showProgress = true;}
		void disableShowProgress() {showProgress = false;}
		void enableMidCompilationFilesystemDebug() {debugMidComp = true;}
		void disableMidCompilationFilesystemDebug() {debugMidComp = false;}
		int emulate(string path);
	};
}

#endif